// const global_url = 'http://192.168.2.144:9001/'
const global_url = __DEV__ ? 'https://ulcadmin.ulmsale.com/' : 'https://ulcadmin.ulmsale.com/'
// const token = 'f7iXjYqmew9QRgY631'
import {storage} from './storage'
import {makeObjToFormData} from './DataUtil'

export default class FetchUtil {

    static async fetchNet(url,data,headers,method) {
        let full_url = global_url+url;
        let token = await storage.load('token')
        console.log('[token]',token)

        let res = await fetch(full_url,{
            method: method,
            ...headers,
            headers: {
                token:token
            },
            body: data
        })
        console.log(665,res)
        let responseData = res.json()
        if(res.status == 200) {
            console.log('[responseData]',responseData)
            return responseData;
        }else{
            return {"errMsg":responseData.message};
        }
        
        
    }

    static get (url,data) {
        let headers = {
            headers:{
                'Content-Type': 'application/json',
            }
        }
        return this.fetchNet(url,data,headers,'GET');
    }

    static post (url,data) {
        let headers = {
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }
        let form_data = new FormData();
        makeObjToFormData(data,form_data);
        console.log('[form_data]',form_data)
        return this.fetchNet(url,form_data,headers,'POST');
    }
}